﻿#include "PixelShader.h"
#include "Graphics.h"
#include "Misc.h"
#include "WinFunc.h"

namespace Base {

	PixelShader::PixelShader(const char* relativePath, const char* entry) {
		this->Init(relativePath, entry);
	}

	void PixelShader::Init(const char* relativePath, const char* entry) {
		auto graphics = Graphics::GetInstance();
		auto device = graphics->GetDevice();

		DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
		shaderFlags |= D3DCOMPILE_DEBUG;
#endif

		// 创建像素着色器
		ComPtr<ID3DBlob> errorBuffer;
		D_HRESULT(D3DCompileFromFile(
			WinFunc::StrToWstr(relativePath).c_str(),
			nullptr,
			nullptr,
			entry,
			"ps_4_0",
			shaderFlags,
			0,
			&m_psBuffer,
			&errorBuffer
		));
		if (errorBuffer != nullptr)
		{
			auto msg = reinterpret_cast<char*>(errorBuffer->GetBufferPointer());
			Debug::ThrowOrMessageBox(msg);
		}
		D_HRESULT(device->CreatePixelShader(
			m_psBuffer->GetBufferPointer(),
			m_psBuffer->GetBufferSize(),
			0,
			&m_pixelShader));
	}
}